#include "Circle.h"


Circle::Circle(){}


Circle::Circle(std::string id, Point center,int radius, Color *borderColor):radius(radius){
	this->center=center;
	this->borderColor = borderColor;	
	this->id=id;
	this->fillColor=NULL;
	this->texture=NULL;
}

Circle::Circle(std::string id, Point center, int radius, Color *lineColor, Color *fillColor):radius(radius){
	this->center=center;
	if(lineColor==NULL)
		this->borderColor = new Color(*fillColor);
	else
		this->borderColor = lineColor;
	this->id=id;
	this->fillColor=fillColor;
	this->texture=NULL;
}

Circle::Circle(std::string id, Point center, int radius, Color *borderColor, Texture* texture):radius(radius){
	this->center=center;
	this->borderColor = borderColor;	
	this->id=id;
	this->fillColor=NULL;
	this->texture=texture;
}

Circle::Circle(std::string id, Point center, int radius, Texture* texture):radius(radius){
	this->center=center;
	this->borderColor = NULL;	
	this->id=id;
	this->fillColor=NULL;
	this->texture=texture;
};


void Circle::draw() throw (DrawingExcep){
	if(fillColor!=NULL)
		SurfaceContainer::getInstance()->fillCircle(this->center,this->radius,this->fillColor->getSDLColor());
	if(texture!=NULL)
		drawTextured();
	if(borderColor!=NULL)
		SurfaceContainer::getInstance()->drawCircle(Point(center.getX(),center.getY()), radius, borderColor->getSDLColor());
	
}


std::string Circle::getCreationLog(){
	std::string msj = "Se creo un Circulo id: '";
	std::stringstream ss; 
	ss << id;
	msj+= ss.str();
	msj.append("'. Radio:");
	ss.str("");
	ss<< radius;
	msj += ss.str();
	msj.append(", Centro: [");
	ss.str("");
	ss<< center.getX();
	msj += ss.str();
	ss.str("");
	msj.append(",");
	ss<< center.getY();
	msj += ss.str();
	msj.append("]");
	return msj;
}

void Circle::drawTextured(){
	Point centro_BMP = *(texture->getImage()->getImageCenter());	
	int x1=0;
	int x2=0;
	int radio_BMP;
	if (texture->getImage()->getImageHeight()<radius+5||texture->getImage()->getImageWidth()<radius+5){
		if(texture->getImage()->getImageHeight()<texture->getImage()->getImageWidth())
			radio_BMP = floor(texture->getImage()->getImageHeight()/2)-5;
		else
			radio_BMP = floor(texture->getImage()->getImageWidth()/2)-5;
	}
	else
		radio_BMP = this->radius;
	
		//nos fijamos antes de graficar que el centro caiga dentro de la pantalla, sino no graficamos nada
	if(!SurfaceContainer::getInstance()->pointInScreen(center))
		throw DrawingExcep(MsjExcep::OutOfScreen(center.getX(),center.getY()));
	//if (!SurfaceContainer::getInstance()->acceptRadius(center,radius))
	//	throw DrawingExcep(MsjExcep::CircleOutOfScreen(center,radius));	

	int counter = (this->center.getY() + this->radius) ;
	
	int posicion =0;
	
	for(int count=(this->center.getY()-this->radius);count<=counter;count++){
		x1=int(this->center.getX()+sqrt((this->radius*this->radius)-((count-this->center.getY())*(count-this->center.getY())))+0.5);
		x2=int(this->center.getX()-sqrt((this->radius*this->radius)-((count-this->center.getY())*(count-this->center.getY())))+0.5);
		
		int sx1, sy1, sx2, sy2;
		float relacion = this->radius / radio_BMP;
		sx1 = centro_BMP.getX() - (((x2-x1)/relacion)/2);
		sx2 = centro_BMP.getX() + (((x2-x1)/relacion)/2);
		sy1 = centro_BMP.getY() - ((this->center.getY() - count) / relacion);
		sy2 = sy1;
		SurfaceContainer::getInstance()->drawTexturedLine(this->texture->getImage(), x1, x2, count, sx1, sy1, sx2, sy2, posicion);	
	}
}

Circle::~Circle(){}